<?php
class Lead_EDO_Finder
{

    /**
     * Lead_EDO_Object_Abstract
     *
     * @var Lead_EDO_Object_Abstract
     */
    protected $_obj;
    
    protected $_handler;
    

    /**
     * Query object
     *
     * @var Query
     */
    protected $_query;

    protected $_joined = array();

    public function __construct(Lead_EDO_Object_Abstract $obj)
    {
        $this->_obj = $obj;
        $this->_handler = $obj->getHandler();

        Zend_Loader::loadClass('Lead_Sql_Query');
        $this->reset();
    }


    public function reset()
    {
        $this->_query = new Lead_Sql_Query($this->_handler->db, false);
                
        $this->_query->addWhereGroup('AND');
        $this->_joined = array();

        /*
        foreach($this->_obj->getTables() as $tableName)
        {
            $table = $this->_obj->db->getTable($tableName);

            $this->_query->addJoin($table, 'INNER', "edo.id = {$tableName}.id");
        }
        */

        $this->_query->getCurrentWhereGroup()->addWhereCond('edo.type_id', '=', "{$this->_obj->getTypeid()}");

        $this->_query->addField('edo.id', true);
    }


    public function getQuery()
    {
        return $this->_query;
    }


    public function getSQL()
    {
        return $this->_query->getSQL();
    }


    public function join($through)
    {
        $this->_obj->getFieldObject($through)->joinTo($this->_query);
    }


    public function addJoin($table, $joinType, $joinClause, $alias = false)
    {
        if(!in_array($table, $this->_joined))
            $this->_query->addJoin($table, $joinType, $joinClause, $alias);

        $this->_joined[] = $table;

    }



    public function addCondition($field, $operator, $value)
    {
        if(!$this->_obj->fieldExists($field))
            throw new Exception("Field '{$field}' does not exist.");


        $table = $this->_obj->getType()->getFieldsTable($field);

        $this->addJoin($table, 'INNER', "edo.id = {$table}.id");


        if($this->_handler->valueIsQuoted($this->_obj->$field()))
        {
            $value = '\'' . $value . '\'';
        }

        $this->_query->getCurrentWhereGroup()->addWhereCond("{$table}.{$field}", $operator, $value, false);

    }



    public function addOrderer($orderer)
    {
        $this->_query->addOrderer($orderer);

        $orderer = explode(',', $orderer);
        foreach($orderer as &$ordeal)
        {
            $ordeal = trim($ordeal);
            $ordeal = explode(' ', $ordeal);

            $table = $this->_obj->getType()->getFieldsTable($ordeal[0]);

            $this->addJoin($table, 'INNER', "edo.id = {$table}.id");

        }



    }



    public function findById($args = array())
	{


	}
	
	
	public function findOne($args = array())
	{
	    
	}
	


	public function findFirst($args = array())
	{
        foreach($args as $arg) {
            $this->addCondition($arg[0], $arg[1], $arg[2]);
        }
        $result = $this->_handler->getDB()->fetchOne($this->_query->getSQL());
        return ($result !== false) ? $this->_obj->get()->fetch($result) : null;
	}



    public function findAll($args = array(), $orderer = false, $collType = 'basic')
    {

        foreach($args as $arg)
        {
            $fObj = $this->_obj->getFieldObject($arg[0]);
            if(!$fObj)
            {
                throw new Exception("Invalid fieldname");
            }

            $this->addCondition($arg[0], $arg[1], $arg[2]);

        }

        if($orderer)
            $this->addOrderer($orderer);
                    
        $result = $this->_handler->getDB()->fetchCol($this->_query->getSQL());

        $coll = $this->_obj->collection($result, $collType);

        return $coll;

    }








}